#!/usr/bin/env bash

# I'm sick and tired of all the churn the three versions of autoconf
# are causing in this repo. Stop committing the configure scripts
# and just autoregen.
./autogen.sh || exit 1

# where are we?
top=$(pwd)
if test -z "${top}" ; then exit 1; fi

# fix
chmod +x vs/sdl/build-scripts/strip_fPIC.sh

if [ -f ${MSYSTEM_PREFIX}/i686-7.3.0-release-posix-dwarf-rt_v5-rev0.txt ] ; then
 echo "set flags for lowend build"
 PATH="${MSYSTEM_PREFIX}/bin:${MSYSTEM_PREFIX}/i686-w64-mingw32/bin:${MSYSTEM_PREFIX}/opt/bin:${PATH}"
 CXXFLAGS="-I${MSYSTEM_PREFIX}/include -I${MSYSTEM_PREFIX}/i686-w64-mingw32/include $CXXFLAGS -I${MSYSTEM_PREFIX}/opt/include "
 CPPFLAGS="-I${MSYSTEM_PREFIX}/include -I${MSYSTEM_PREFIX}/i686-w64-mingw32/include $CPPFLAGS -I${MSYSTEM_PREFIX}/opt/include "
 CFLAGS="-I${MSYSTEM_PREFIX}/include -I${MSYSTEM_PREFIX}/i686-w64-mingw32/include $CFLAGS -I${MSYSTEM_PREFIX}/opt/include "
 LDFLAGS="-L${MSYSTEM_PREFIX}/lib -L${MSYSTEM_PREFIX}/i686-w64-mingw32/lib -L${MSYSTEM_PREFIX}/opt/lib $LDFLAGS "
 export LDFLAGS PATH
 sed -i 's@\(^#if !defined(__MINGW32__)\)\(.*|| defined(__MINGW64_VERSION_MAJOR)\)@//\1\2 Fix for lowend build. Revert this for normal versions\n\1@g' src/hardware/serialport/misc_util.cpp
fi
export CXXFLAGS CPPFLAGS CFLAGS
echo "path=$PATH"

sed -i 's/^#define ENABLE_IM_EVENT 1$/\/\/#define ENABLE_IM_EVENT 1/g' vs/sdl/include/SDL_platform.h

# prefer to compile against our own copy of SDL 1.x
echo "Compiling our internal SDL 1.x"
(cd vs/sdl && ./build-dosbox.sh) || exit 1

# prefer to compile against our own copy of SDLnet 1.x
echo "Compiling our internal SDLnet 1.x"
(cd vs/sdlnet && ./build-dosbox.sh) || exit 1

sed -i 's/^\/\/#define ENABLE_IM_EVENT 1$/#define ENABLE_IM_EVENT 1/g' vs/sdl/include/SDL_platform.h
(cd vs/sdl && ./build-dosbox.sh) || exit 1

# NTS: MinGW provides zlib for us
if false; then
	# prefer to compile against our own zlib
	echo "Compiling our internal zlib"
	(cd vs/zlib && ./build-dosbox.sh) || exit 1
	new="-I${top}/vs/zlib/linux-host/include "
	nld="-L${top}/vs/zlib/linux-host/lib "
	CFLAGS="${new}${CFLAGS}"
	LDFLAGS="${nld}${LDFLAGS}"
	CPPFLAGS="${new}${CPPFLAGS}"
	CXXFLAGS="${new}${CXXFLAGS}"
	export CFLAGS LDFLAGS CPPFLAGS CXXFLAGS
fi

# prefer to compile against our own libpng (comment this out to disable)
echo "Compiling our internal libpng"
(cd vs/libpng && ./build-dosbox.sh) || exit 1
new="-I${top}/vs/libpng/linux-host/include "
nld="-L${top}/vs/libpng/linux-host/lib "
CFLAGS="${new}${CFLAGS}"
LDFLAGS="${nld}${LDFLAGS}"
CPPFLAGS="${new}${CPPFLAGS}"
CXXFLAGS="${new}${CXXFLAGS}"
export CFLAGS LDFLAGS CPPFLAGS CXXFLAGS

# prefer to compile against our own freetype
echo "Compiling our internal freetype"
(cd vs/freetype && ./build-dosbox.sh) || exit 1
new="-I${top}/vs/freetype/linux-host/include/freetype2 "
nld="-L${top}/vs/freetype/linux-host/lib -lfreetype "
CFLAGS="${new}${CFLAGS}"
LDFLAGS="${nld}${LDFLAGS}"
CPPFLAGS="${new}${CPPFLAGS}"
CXXFLAGS="${new}${CXXFLAGS}"
INTERNAL_FREETYPE=1
export CFLAGS LDFLAGS CPPFLAGS CXXFLAGS INTERNAL_FREETYPE

# pacman -S --needed --noconfirm mingw-w64-x86_64-libslirp &>/dev/null
CXXFLAGS="$CXXFLAGS -DNCURSES_STATIC "
CPPFLAGS="$CPPFLAGS -DNCURSES_STATIC "
CFLAGS="$CFLAGS -DNCURSES_STATIC "
export CXXFLAGS CPPFLAGS CFLAGS

# now compile ourself
echo "Compiling DOSBox-X"
chmod +x configure
./configure --enable-debug --disable-avcodec --enable-d3d9 --enable-d3d-shaders --disable-libfluidsynth --prefix=/usr "${@}" || exit 1
make -j3 || exit 1
